package com.btpj.module_main.ui.nav

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.btpj.jetpack.base.BaseViewModel
import com.btpj.jetpack.utils.ToastUtil
import com.btpj.jetpack.entity.ApiResult
import com.btpj.module_main.data.entity.NavItem
import com.btpj.module_main.data.remote.repository.MainRepository
import kotlinx.coroutines.launch

class NavViewModel(private val repository: MainRepository) : BaseViewModel() {
    val navItemLiveData = MutableLiveData<MutableList<NavItem>>()

    override fun start() {
        getNavigationData()
    }

    private fun getNavigationData() {
        viewModelScope.launch {
            when (val result = repository.getNavigationData()) {
                is ApiResult.Success -> navItemLiveData.postValue(result.data)
                is ApiResult.Error -> ToastUtil.showShort(result.exception.errorMsg ?: "请求失败")
            }
        }
    }
}